package com.bright.hxj.cwduijie.service.impl;

import com.bright.ghj.common.util.StringUtil;
import com.bright.hxj.cwduijie.converter.DebtPublishConverter;
import com.bright.hxj.cwduijie.enums.DataApi;
import com.bright.hxj.cwduijie.enums.SJJHBZ;
import com.bright.hxj.cwduijie.pojo.bo.ApiDataCollection;
import com.bright.hxj.cwduijie.pojo.dto.DebtPublishDto;
import com.bright.hxj.cwduijie.pojo.po.DebtPublish;
import com.bright.hxj.cwduijie.pojo.query.DuijieQuery;
import com.bright.hxj.cwduijie.pojo.vo.DebtPublishVO;
import com.bright.hxj.cwduijie.repository.DebtPublishRepository;
import com.bright.hxj.cwduijie.service.AbstractMonthlyReportApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @Author hxj
 * @Date 2024/7/29 14:42
 * @Description
 */

@Component
@RequiredArgsConstructor
@Slf4j
public class ApiDebtPublishServiceImpl extends AbstractMonthlyReportApiService<DebtPublishDto, DebtPublishVO> {

    private final DebtPublishRepository debtPublishRepository;
    private final DebtPublishConverter debtPublishConverter;

    @Override
    public JpaSpecificationExecutor getJpaSpecificationExecutor() {
        return debtPublishRepository;
    }

    @Override
    public DataApi getDataApi() {
        return DataApi.DEBT_PUBLISH;
    }

    @Override
    public Function<DebtPublishDto, Integer> getIdFunc() {
        return DebtPublishDto::getId;
    }

//    @Override
//    public List<String> getNeedDuijieZtDataKeys() {
//        List<String> dataKeys = debtPublishRepository.getDistinctZtDataKeyBySjjhbz(SJJHBZ.WAITING.getValue());
//        dataKeys.addAll(debtPublishRepository.getDistinctZtDataKeyBySjjhbz(SJJHBZ.ERROR.getValue()));
//        return dataKeys;
//    }

    @Override
    protected void getData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery) {
        List<String> yearMonths = debtPublishRepository.getDistinctSSQJByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.WAITING.getValue());
        yearMonths.addAll(debtPublishRepository.getDistinctSSQJByZtDataKeyAndSjjhbz(duijieQuery.getZtDataKey(), SJJHBZ.ERROR.getValue()));
        Map<String, List<DebtPublishDto>> result = new HashMap<>();

        // 20240725 加上判断是否有指定对接年月 若有指定就只返回指定年月的数据 没有就返回全部
        // 比如只指定了2024年 则selectedYearMonth=2024  若指定2024年1月 则=202401
        String selectedYearMonth = getSelectedYearMonth(duijieQuery);

        for (String yearMonth : yearMonths) {
            if (selectedYearMonth == null || StringUtil.startsWith(yearMonth, selectedYearMonth)) {
                // 只推送选定年月的数据
                List<DebtPublish> balances = debtPublishRepository.getByZTDATAKEYAndSSQJ(duijieQuery.getZtDataKey(), yearMonth);
//            balances.addAll(balanceRepository.getByZTDATAKEYAndSSQJ(duijieQuery.getAccSetDataKey(), yearMonth, SJJHBZ.ERROR.getValue()));
                result.put(yearMonth, debtPublishConverter.poConvertDTO(balances));
            }

        }

        apiDataCollection.setDebtPublishMap(result);
    }

    @Override
    public Function<ApiDataCollection, Map<String, List<DebtPublishDto>>> getDataFunc() {
        return ApiDataCollection::getDebtPublishMap;
    }

    @Override
    public Function<DebtPublishDto, DebtPublishVO> getConverterFunc() {
        return debtPublishConverter::dtoConvertVO;
    }

    @Override
    public String getYearMonthColumnName() {
        return "SSQJ";
    }
}
